var app = getApp()
import { validateData } from '../../utils/util'
import { showToast, showLoading,  redirectTo,relaunch, chooseLocation } from '../../utils/promiseFns'
import { postUserInfo, setParams, dataQuery } from '../../utils/apiFns'


Page({
    data: {
        errMsg: null,
        userInfo: {
            name: '',
            phone: '',
            idcard: '',
            dest: '',
            bus:''
        },
        departure: '',
        departurePoint:{
            lat:0,
            lng:0
        },
        map: null
    },
    postCount :0,
    chooseLocation(e) {

        var that = this;
        var type = e.target.dataset.type;


        wx.showLoading({
            title: '正在加载地图..',
            mask: true
        })

        chooseLocation().then(res => {

            this.setData({
                departure: res.address,
                departurePoint:{
                    lng: res.longitude,
                    lat: res.latitude
                }
            })

            // this.setUserInfoData(type, res.address)
            // this.setUserInfoData(type + 'Point', {
            //     lng: res.longitude,
            //     lat: res.latitude
            // })


        }).catch(err => {

            wx.showToast({
                title: err.toString(),
                image: '/resources/images/error3.png',
                duration: 3000
            })

        })

    },
    setUserInfoData(key, value) {

        var userInfoData = this.data.userInfo;
        var tempData = {}
        tempData[key] = value
        userInfoData = Object.assign({}, userInfoData, tempData)
        
        this.setData({
            userInfo: userInfoData
        })

    },
    publish() {

        var isValidatePass = true;
        var item;

        var userInfo = Object.assign({},this.data.userInfo,{
            departure:this.data.departure,
            departurePoint:this.data.departurePoint
        })
        
        var result = validateData(userInfo)

        this.setData({
            errMsg: result
        })

        for (item in result) {
            if (result[item]) {
                isValidatePass = false;
            }
        }

        if (isValidatePass && this.postCount == 0) {

            this.postCount = 1;

            this.postData(userInfo);

        }else if(isValidatePass && this.postCount != 0){

            showToast('请勿重复提交','','/resources/images/error3.png')

        }

    },

    inputText(e) {
        var inputId = e.target.id;
        // app.orderData[inputId] = e.detail.value;
        this.setUserInfoData(inputId, e.detail.value)
    },

    postData(data) {

        wx.setStorage({
            key: 'userInfo',
            data: {
                name: data.name,
                phone: data.phone,
                idcard: data.idcard
            }
        })

        showLoading()

        var postParams = setParams('addServiceRequestTravel', {
            token: app.user_token,
            name: data.name,
            phone: data.phone,
            id_type: 1,
            id_num: data.idcard,
            dep_address: data.departure,
            dep_lng: data.departurePoint.lng,
            dep_lat: data.departurePoint.lat,
            dest_address: data.dest,
            bus:data.bus
        })

        dataQuery(postParams)()
            .then(res => {
                return showToast('提交成功', 'success')
            })
            .then(res => {
                relaunch('/pages/index/index')
            })
    },

    onLoad() {


        var userInfo = wx.getStorageSync('userInfo')

        if (userInfo) {
            this.setData({
                userInfo: {
                    name: userInfo.name,
                    phone: userInfo.phone,
                    idcard: userInfo.idcard
                }
            })
        }
    },

    onShow() {
        this.postCount = 0;
        wx.setNavigationBarTitle({
            title: '我的出行'
        })

    // app.clearOrderData();
    },



});